﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.IO;
namespace SlideShow {
    /// <summary>
    /// Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {

        ThicknessAnimation ani;
       DoubleAnimation zoon;
       ThicknessAnimation salir;
       Image img, aux2, aux;
       bool semaforo = false;
        Stack<Image> imagenes = new Stack<Image>();
                public MainWindow() {
            InitializeComponent();
            ani = new ThicknessAnimation();
            ani.From = new Thickness(526, 63, -366, 26);
            ani.To = new Thickness(78,53,82,37);
            ani.Duration= new Duration(new TimeSpan(5500000));
            ani.AccelerationRatio = 0.1;
            ani.DecelerationRatio = 0.5;

            zoon = new DoubleAnimation();
            zoon.From = 200;
            zoon.To = 300;
            zoon.Duration = new Duration(new TimeSpan(55000000));
            zoon.BeginTime = new TimeSpan(5500000);
            zoon.AutoReverse = true;
            zoon.Completed += h;
            zoon.AccelerationRatio = 0.3;
            zoon.DecelerationRatio = 0.1;
            
           // img.MouseEnter += h;

            salir = new ThicknessAnimation();
            salir.From = new Thickness(78, 53, 82, 37);
            salir.To = new Thickness(-2000, 53, 82, 37);
            salir.Duration = new Duration(new TimeSpan(5500000));
            salir.AccelerationRatio = 0.3;
            salir.Completed += remover;
          salir.BeginTime = new TimeSpan(1500000);
           
        }
    
    public void iniciarAnimacion(){

        String [] imgs= Directory.GetFiles("C:/My/BabyShower");
        for (int i = 0; i < imgs.Length;i++ ) {
            img = new Image();
            try {
                img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(imgs[i]);
                img.Margin = new Thickness(526, 63, -366, 26);
                img.Width = 200;
                img.Height = 200;
                imagenes.Push(img);
            } catch (Exception e) { }
          }
   aux= imagenes.Pop();
   panel.Children.Add(aux);
   aux.BeginAnimation(MarginProperty, ani);
   aux.BeginAnimation(WidthProperty, zoon);
   aux.BeginAnimation(HeightProperty, zoon);
   //aux.BeginAnimation(MarginProperty, salir); 
    }

    private void Button_Click(object sender, RoutedEventArgs e) {
        iniciarAnimacion();
    }

        private void h(object sender, EventArgs arg) {
            if (semaforo) {
                semaforo = false;
                return;
            }
            semaforo = true;
         aux2=aux;
        aux2.BeginAnimation(MarginProperty, salir);
      //  panel.Children.Remove(aux2);
        aux2 = null;
        try {
            aux = imagenes.Pop();
        }catch(Exception e){
            return;
        }
        panel.Children.Add(aux);
        aux.BeginAnimation(MarginProperty, ani);
        aux.BeginAnimation(WidthProperty, zoon);
        aux.BeginAnimation(HeightProperty, zoon);
       // aux.BeginAnimation(MarginProperty, salir); 
   
    }

        private void remover(object sender, EventArgs arg) {
            panel.Children.Remove(aux2);   
        }
        

    
    }
}
